const app = getApp();
const Api = app.globalData.Api;
const wxApi = app.globalData.wxApi;
const util = app.globalData.util;
// const util = app.globalData.util;
Page({
  data: {
    preMemories: [],
    preIndex: 0,
    memoryVideos: []
  },
  onLoad( options ) {
    this.init( options );
  },
  init( options ) {
    this.pageNo = 1;
    let preMemories = util.decodeURI( options.memories );
    wxApi.setNavTitle('记忆法大全')
    this.setData({
      preMemories: preMemories
    })
    this.refresh();
  },
  /**
   * 点击tab栏
   */
  onPreTabClick( e ) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      preIndex: index
    })
    console.log( e );
  },
  gotoVideoDetail( e ) {
    let video = e.currentTarget.dataset.video;
    wxApi.navigateTo('../memory-detail/memory-detail', {
      id: video.id
    })
  },
  refresh() {
    this.pageNo = 1;
    this.loadMemoryVideos( this.pageNo, videos => {
      console.log(videos)
      this.setData({
        memoryVideos: videos
      })
    } )
  },
  /**
   * 加载视频列表
   */
  loadMemoryVideos( pageNo, callback ) {
    let params = {
      pageNo: pageNo || 1,
      pageSize: 20
    }
    Api.memoryVideos(params, res => {
      callback && callback( res.records )
    })
  }
}) 